﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BCCommon
{
    /// <summary>
    /// 随机码生成类
    /// </summary>
    public class RandomCode
    {
        /// <summary>
        /// 生成随机字符串。
        /// </summary>
        /// <param name="length">长度。</param>
        /// <returns></returns>
        public static string GenerateUniqueCode(int length = 8)
        {
            string readyStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char[] rtn = new char[length];
            Guid gid = Guid.NewGuid();
            byte[] ba = gid.ToByteArray();
            for (int i = 0; i < length; i++)
            {
                rtn[i] = readyStr[((ba[i] + ba[8 + i]) % 35)];
            }
            string renstr = string.Empty;
            for (int i = 0; i < rtn.Length; i++)
            {
                renstr += rtn[i];
            }
            return renstr;
        }

        /// <summary>
        /// 生成数字随机验证码。
        /// </summary>
        /// <param name="length">长度。</param>
        /// <returns></returns>
        public static string GeneratePassCode(int length = 6)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            string validateNumberStr = "";
            //生成起始序列值  
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字  
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字  
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码  
            for (int i = 0; i < length; i++)
            {
                validateNumberStr += validateNums[i].ToString();
            }
            return validateNumberStr;
        }
        /// <summary>
        /// 获取一个支付使用的订单号。
        /// </summary>
        /// <returns></returns>
        public static string GeneratePaymentOrderNo()
        {
            string uniqueCode = GenerateUniqueCode(6);

            return string.Format("BR{0:yyyyMMdd}{1}", DateTime.Now, uniqueCode).ToUpperInvariant();
        }

        /// <summary>
        /// 获取销售订单编号
        /// </summary>
        /// <param name="prefix">订单号前缀。</param>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        public static string GetOrderNo(string prefix, long id)
        {
            string uniqueCode = GenerateUniqueCode();

            return string.Format("{0}{1:yyMM}{2:0000000000}", prefix, DateTime.Now, id.ToString());
        }

        /// <summary>
        /// 获取订单编号
        /// </summary>
        /// <param name="cityCode">城市编码</param>
        /// <returns></returns>
        public static string GetOrderNo(string cityCode)
        {
            string uniqueCode = GenerateUniqueCode();

            return string.Format("{0}{1:yyMM}{2}", cityCode.Substring(0, 4), DateTime.Now, uniqueCode);
        }

        /// <summary>
        /// 获取一个订单号。
        /// </summary>
        /// <param name="prefix">订单号前缀。</param>
        /// <param name="orangizationId">合伙人编号</param>
        /// <returns></returns>
        public static string GenerateOrderNo(string prefix, long orangizationId)
        {
            string uniqueCode = GenerateUniqueCode(6);

            return string.Format("{0}{1:00000}{2:yyMM}{3}", prefix, orangizationId, DateTime.Now, uniqueCode).ToUpperInvariant();
        }
        /// <summary>
        /// 获取一个订单号。
        /// </summary>
        /// <param name="prefix">订单号前缀。</param>
        /// <param name="countyCode">区县编码</param>
        /// <returns></returns>
        public static string GenerateOrderNo(string prefix, string countyCode)
        {
            string uniqueCode = GenerateUniqueCode(6);

            return string.Format("{0}{1:00000}{2:yyMM}{3}", prefix, countyCode, DateTime.Now, uniqueCode).ToUpperInvariant();
        }

        /// <summary>
        /// 获取一个收货码
        /// </summary>
        /// <returns></returns>
        public static string GenerateReceivingCode()
        {
            return GenerateUniqueCode(6);
        }

    }
}
